package be.pluyckx.batteryprogress.utils;

/**
 * Created by Philip on 14/09/13.
 */
public class StringUtils {
  private StringUtils() {

  }

  public static int lengthUTF(String s, boolean addLengthField) {
    int length = 0;

    for (int i = 0; i < s.length(); i++) {
      Character ch = s.charAt(i);

      if (ch <= 0x80) {
        length++;
      } else if (ch <= 0x800) {
        length += 2;
      } else if (ch <= 0x8000) {
        length += 3;
      } else {
        length += 4;
      }
    }

    if (addLengthField) {
      length += 2;
    }

    return length;
  }
}
